package com.quasar.hpatient.view.personal.historyRecord;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.custommodel.BagFileDataBean;
import com.quasar.hpatient.model.custommodel.BagFileItem;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.medicalmodel.ArchivesData;
import com.quasar.hpatient.onInterface.viewInterface.UiInterface;
import com.quasar.hpatient.presenter.BagFilePresenter;
import com.quasar.hpatient.utils.DateUtil;
import com.quasar.hpatient.view.adapter.BagFileAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 档案袋
 */
@EActivity(R.layout.activity_bag_file)
public class BagFileActivity extends BaseActivity implements UiInterface<AnotherResult<ArchivesData>>, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private BagFilePresenter bagFilePresenter;
    private BagFileAdapter bagFileAdapter;
    private int delayMillis = 1000;//设置刷新等待的时间

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void initData() {
        bagFilePresenter = new BagFilePresenter(this);
        initAdapter();
    }


    //初始化adapter布局
    public void initAdapter() {
        bagFileAdapter = new BagFileAdapter();
        bagFileAdapter.setOnLoadMoreListener(this, rv_list);
        rv_list.setAdapter(bagFileAdapter);
        //获取数据
        bagFilePresenter.PostBagFile(page+"");

        //  ImageViewpageActivity_.intent(TestRecordActivity.this).Photolist(photos).position(position).start();
    }

    @Override
    public void onViewSuccess(AnotherResult<ArchivesData> data) {
        List<BagFileDataBean> bagFileDataBeen = new ArrayList<>();
        if (data.getList() != null) {

            Map<String, String> map = new HashMap<>();
            for (int i = 0; i < data.getList().size(); i++) {
                map.put(data.getList().get(i).getTestTime(), data.getList().get(i).getCover());
            }
            Map<String, List<BagFileItem>> map1 = new HashMap<>();
            for (String time : map.keySet()) {
                List<BagFileItem> co = new ArrayList<>();
                for (int i = 0; i < data.getList().size(); i++) {
                    if (time.equals(data.getList().get(i).getTestTime())) {
                        BagFileItem bagFileItem = new BagFileItem();
                        bagFileItem.setCover(data.getList().get(i).getCover());
                        co.add(bagFileItem);
                    }
                    map1.put(time, co);
                }
            }
            String json = JSON.toJSONString(map1);
            ViseLog.d(json);
            for (Map.Entry<String, List<BagFileItem>> entry : map1.entrySet()) {
//
                BagFileDataBean bb = new BagFileDataBean();
                bb.setTime(entry.getKey()); //把键存入
                bb.setBagFileItems(entry.getValue());  //把图片信息存入
                bagFileDataBeen.add(bb);
            }
        }

        //对分组出来数据进行排序
        Collections.sort(bagFileDataBeen, new Comparator<BagFileDataBean>() {
            @Override
            public int compare(BagFileDataBean o1, BagFileDataBean o2) {
                Date date1 = DateUtil.stringToDate(o1.getTime());
                Date date2 = DateUtil.stringToDate(o2.getTime());
                if (date1.before(date2)) {
                    return 1;
                }
                return -1;
            }
        });
        //判断page是否是下拉还是上拉
        if(page==1){
            bagFileAdapter.setNewData(bagFileDataBeen);
            onLoadMore=false;
        }else{
            bagFileAdapter.addData(bagFileDataBeen);
            if(data.getList().size()==0){
                onLoadMore=true;
            }
        }
    }

    @Override
    public void onViewError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onViewRefreshUI() {
        bagFileAdapter.notifyDataSetChanged();
    }

    int page=1;
    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        page=1;
        bagFilePresenter.PostBagFile(page+"");
        bagFileAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                bagFileAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }
    //判断下一页是否存在数据
    private boolean onLoadMore=false;

    @Override
    public void onLoadMoreRequested() {
        bagFileAdapter.loadMoreEnd();
        page++;//下拉时page递增
        bagFilePresenter.PostBagFile(page+"");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    bagFileAdapter.loadMoreEnd();
                } else {
                    bagFileAdapter.loadMoreComplete();
                    bagFileAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }
}
